# -*- coding = utf-8 -*-
# @Time : 2022/9/27 10:09
# @Author :Henry L
# @File : PicApi.py
# @Software : PyCharm
import os

import uvicorn
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
import requests

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}


pid = ""

class Hero(BaseModel):
    type: str
    server: str


@app.post("/StartPythonScript/")
def run_python_script(value: Hero):
    spider_dict = {"1": "192.168.13.82：8004", "2":"192.168.14.18:8004","3":"192.168.13.82:8004","4":"192.168.11.61:8004",
                   "5":"192.168.11.250:8004","6":"192.168.14.232:8004","7":"192.168.14.130:8004","8":"192.168.12.129:8004",
                   "9":"192.168.13.145:8004","10":"192.168.13.167:8004","11":"192.168.14.118:8004","12":"192.168.13.254:8004",
                   "13":"192.168.13.176:8004"}
    server = value.server.split(",")
    status = ""
    pid = os.getpid()
    print(pid)
    try:
        if value.type == "30":
            # 确保使用正确的 URL 和端口
            for i in server:
                if i== "3":
                    url = "http://{}/run_fixed_python_script".format(spider_dict[i])
                    response = requests.post(url)
                    status = response.status_code
                if status == 200:
                    return {"status": "success", "message": "Python脚本执行成功"}
                else:
                    return {"status": "error", "message": f"请求返回状态码: {status}"}
    except Exception as e:
        return {"status": "error", "message": f"请求失败: {e}"}


class Stop(BaseModel):
    server: str


@app.post("/StopPythonScript")
def stop_python_script(value: Stop):
    spider_dict = {"1": "192.168.13.82：8004", "2": "192.168.14.18:8004", "3": "192.168.13.82:8004",
                   "4": "192.168.11.61:8004",
                   "5": "192.168.11.250:8004", "6": "192.168.14.232:8004", "7": "192.168.14.130:8004",
                   "8": "192.168.12.129:8004",
                   "9": "192.168.13.145:8004", "10": "192.168.13.167:8004", "11": "192.168.14.118:8004",
                   "12": "192.168.13.254:8004",
                   "13": "192.168.13.176:8004"}
    server = value.server.split(",")
    status = ""
    try:
        # 确保使用正确的 URL 和端口
        for i in server:
            ## 先这样，等下需要把3给弄掉，测试的时候就这样这样子搞
            if i == "3":
                url = "http://{}/end_task".format(spider_dict[i])
                response = requests.post(url)
                status = response.status_code
        if status == 200:
            return {"status": "success", "message": "Python脚本执行成功"}
        else:
            return {"status": "error", "message": f"请求返回状态码: {status}"}
    except Exception as e:
        return {"status": "error", "message": f"请求失败: {e}"}


def run():
    uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")




if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")
